<script setup lang="ts">
// import { adminFaPastureListApi } from '@/api/fa/pasture'
import { spForageInOutRfidBatchDelApi, spForageInOutRfidListApi } from '@/api/cy/FoodGoTo'
import { useTable } from '@/hooks/web/useTableTest'
import { PropType, reactive, ref } from 'vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { useIcon } from '@/hooks/web/useIcon'
import { ElMessage, ElMessageBox } from 'element-plus'

const iconDel = useIcon({ icon: 'ep:delete' })

const props = defineProps({
  rowid: {
    type: Object as PropType<Nullable<any>>,
    default: () => null
  },
  da: {
    type: Object as PropType<Nullable<any>>,
    default: () => null
  },
  see: String
})
console.log(props, 'RFID')
let rfid = ref<any>()

const RFID: any = async () => {
  // console.log(props, 'e')
  // 查看
  if (props.see) {
    // console.log(!props.see)
    const res = await spForageInOutRfidListApi({
      forage_in_out_detailed_id: props.see,
      pageSize: tableObject.pageSize,
      pageNo: tableObject.currentPage
    })
      .catch(() => {})
      .finally(() => {})
    if (res) {
      tableObject.tableList = res.data
      tableObject.count = res.count
      tableObject.loading = false
    }
  }
  if (props.rowid.forage_in_out_order_id) {
    const res = await spForageInOutRfidListApi({
      forage_in_out_detailed_id: props.rowid.id,
      pageSize: tableObject.pageSize,
      pageNo: tableObject.currentPage
    })
      .catch(() => {})
      .finally(() => {})
    if (res) {
      tableObject.tableList = res.data
      tableObject.count = res.count
      tableObject.loading = false
    }
  }

  if (props.da.rfid) {
    rfid.value = props.da.rfid
    // console.log(props, props.da.rfid, rfid.value, 'vaue')
    Object.entries(props.da.rfid).forEach(([a, b]) => {
      console.log(a)
      if (b) {
        tableObject.tableList.push({ rfid: b })
      }
    })
  }
}
const { register, tableObject, methods } = useTable<
  {
    total: number
    list: any[]
  },
  any
>({
  getListApi: RFID,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { getList } = methods
getList()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'id',
    label: '主键',
    fixed: 'left',
    width: 200,
    table: {
      show: true
    },
    form: {
      show: true
    },
    detail: {
      show: true
    }
  },
  {
    field: 'rfid',
    label: 'rfid',
    // width: 150,
    search: {
      show: true
    }
  },
  {
    field: 'action',
    width: 150,
    fixed: 'right',
    label: '操作',
    form: {
      show: false
    },
    detail: {
      show: false
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const actionType = ref('')

const action = async (type: string, row: any = {}) => {
  actionType.value = type
  switch (type) {
    case 'add':
      ElMessageBox.prompt('添加Rfid, 以逗号分隔', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(({ value }) => {
          rfid.value = value.split(',')
          // tableObject.tableList = []
          Object.entries(rfid.value).forEach(([a, b]) => {
            console.log(a)
            if (b) {
              tableObject.tableList.push({ rfid: b })
            }
          })
          ElMessage({
            type: 'success',
            message: `添加成功:${value}`
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '取消'
          })
        })
      break
    case 'del':
      ElMessageBox.confirm('确定要删除这个RFID吗？', '删除', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          console.log(row, 'row')
          // if (props.rowid.forage_in_out_order_id) {
          //   const res = await spForageInOutRfidBatchDelApi({
          //     forage_in_out_detailed_id: props.rowid.forage_in_out_order_id,
          //     ids: row.id
          //   })
          //     .catch(() => {})
          //     .finally(() => {})
          //   if (res) {
          //     getList()
          //   }
          // }
          let index = ref()
          if (!tableObject.tableList[0].id) {
            Object.entries(rfid.value).forEach(([a, b]) => {
              if (b == row.rfid) {
                index.value = a
              }
            })
            tableObject.tableList.splice(index.value, 1)
            rfid.value.splice(index.value, 1)
          } else {
            const res = await spForageInOutRfidBatchDelApi({
              forage_in_out_detailed_id: props.rowid.id,
              ids: [row.id]
            })
              .catch(() => {})
              .finally(() => {})
            if (res) {
              getList()
            }
          }

          // console.log(index.value, rfid.value, row.rfid, 'row')
        })
        .catch(() => {
          console.log('这里是取消confirm弹框')
        })
      break
    default:
      console.log('未知类型')
  }
}

defineExpose({
  rfid
})
</script>

<template>
  <ContentWrap>
    <div class="mb-10px">
      <!-- <ElButton v-if="props.da" type="primary" @click="action('add')">新增Rfid</ElButton> -->
    </div>
    <Table
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :border="true"
      :pagination="{
        total: tableObject.count
      }"
      @register="register"
      :selection="false"
      highlight-current-row
    >
      <template #action="{ row }">
        <ElButton
          circle
          v-if="!props.see"
          :icon="iconDel"
          @click="action('del', row)"
          title="删除"
        />
        <!-- <ElButton circle :icon="iconAssets" @click="action('assets', row)" title="打标资产" /> -->
      </template>
    </Table>
  </ContentWrap>
</template>
<style scoped>
.btn {
  margin: 10px;
}
</style>
